<!--
 * @Description:
 * @version:
 * @Author: sunqian_sr
 * @Date: 2022-11-29 16:36:58
 * @LastEditors: Andy
 * @LastEditTime: 2023-03-01 11:19:24
-->
<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="40%">
    <el-form
      :model="params"
      class="demo-form-inline"
      ref="params"
      :rules="rules"
      label-position="top"
      label-width="auto"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('organization.rank.chooseOrganization')" prop="orgId">
            <x-select-tree
              v-model="params.orgId"
              :nodeKey="'id'"
              :options="options"
              :defaultProps="defaultProps"
            ></x-select-tree>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="params.parentId + '' !== '0'">
          <el-form-item :label="$t('organization.rank.preRank')" prop="parentId">
            <x-select-tree
              v-model="params.parentId"
              :nodeKey="'id'"
              :options="rankTreeOptions"
              :defaultProps="defaultProps"
            ></x-select-tree>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item :label="$t('organization.rank.rankCode')" prop="rankCode">
              <el-input v-model="params.rankCode" :placeholder="$t('organization.rank.rankCodePlaceholder')"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item :label="$t('organization.rank.rankName')" prop="rankName">
            <el-input
              v-model="params.rankName"
              :placeholder="$t('organization.rank.rankNamePlaceholder')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.rank.rankCode')" prop="rankCode">
            <el-input
              v-model="params.rankCode"
              :placeholder="$t('organization.rank.codePlaceholder')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.rank.rankAbbreviation')" prop="shortName">
            <el-input
              v-model="params.shortName"
              clearable
              :placeholder="$t('organization.rank.rankAbbreviationPlaceholder')"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.rank.rankType')" prop="rankType">
            <x-select
              :placeholder="$t('system.user.pleaseSelect')"
              v-model="params.rankType"
              :options="dictOptions['upms_rank.rank_type']"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item :label="$t('organization.rank.showSort')" prop="showSort">
            <el-input v-model="params.showSort" :placeholder="$t('organization.rank.sortPlaceholder')"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item :label="$t('organization.rank.enable')" prop="state">
            <x-radio-group :listData="dictOptions['system.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading"> {{ $t('common.confirm') }} </el-button>
    </span>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit, throttle } from '@package/utils'
  import { apiPostRankAdd, apiPutRankUpdate } from '../api/rank.js'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  export default {
    props: {
      options: {
        type: Array,
        default: () => []
      },
      rankTreeOptions: {
        type: Array,
        default: () => []
      },
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        visible: false,
        title: this.$t('common.add'),
        params: {
          orgId: '',
          rankName: '',
          state: '1',
          parentId: '',
          rankCode: '',
          // tenantId: '0',
          shortName: '',
          rankType: ''
        },
        rules: {
          rankName: [
            { required: true, message: this.$t('organization.rank.rankNamePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, trigger: 'blur' }
          ],
          parentId: [{ required: true, message: this.$t('organization.rank.preRankPlaceholder'), trigger: 'blur' }],
          orgId: [
            { required: true, message: this.$t('organization.rank.chooseOrganizationPlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, trigger: 'blur' }
          ],
          rankType: [{ required: false, message: this.$t('organization.rank.rankTypePlaceholder'), trigger: 'blur' }],
          shortName: [
            { required: true, message: this.$t('organization.rank.rankAbbreviationPlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, trigger: 'blur' }
          ],
          rankCode: [
            { required: true, message: this.$t('organization.rank.codePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, trigger: 'blur' }
          ],
          state: [{ required: true, message: this.$t('organization.rank.statePlaceholder'), trigger: 'blur' }]
        },
        defaultProps: { children: 'children', label: 'shortName', value: 'id' },
        loading: false
      }
    },
    methods: {
      handleOpen(info) {
        this.visible = true
        if (info?.id) {
          this.title = this.$t('organization.rank.editRank')
        } else {
          this.title = this.$t('organization.rank.addRank')
        }
        this.$nextTick(() => {
          this.params = Object.assign(this.params, cloneDeep(info))
          this.params.state += ''
        })
      },
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.visible = false
      },
      handleSubmit: throttle(function () {
        this.loading = true
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params
            }
            try {
              const { code, msg } = param.id ? await apiPutRankUpdate(param) : await apiPostRankAdd(param)
              this.loading = false
              if (code === EnumApiCode.Success) {
                param.id
                  ? this.$message.success(this.$t('common.editSuccess'))
                  : this.$message.success(this.$t('common.newSuccess'))
                this.$emit('handleSuccess')
                this.handleClose()
              } else {
                this.$message.error(msg)
              }
            } finally {
              this.loading = false
            }
          } else {
            return false
          }
        })
      }, 1000)
    }
  }
</script>
